function [y_predict,accuracy,score] = KNNC(train_data,train_labels,test_data,test_labels,Default)

%二分类
train_labels(train_labels~=0)=1;
test_labels(test_labels~=0)=1;
%多分类
% train_labels=sign(train_labels);
% test_labels=sign(test_labels);

predictions=[];
labels=[];
errors=[];
scores=[];
if Default
    tic
    load("KNNC_BestModel.mat");
    toc
else
    BestModel = fitcknn(train_data, train_labels, 'Standardize', true, ...
        'HyperparameterOptimizationOptions',struct('Optimizer','gridsearch', ...
        'KFold',5,...
        'MaxObjectiveEvaluations',70,...
        'Repartition',true, ...
        'NumGridDivisions',100),...
        'OptimizeHyperparameters','auto');
    save("KNNC_BestModel",'BestModel');
end
[y_predict,score]=predict(BestModel,test_data);
error = zeros(length(test_data),1);
index = y_predict~=test_labels;
error(index) = 1;
accuracy=sum(test_labels==y_predict)./length(test_labels);
resultTable=table(test_data,test_labels,y_predict,error);
filename = 'KNNC.xlsx';
writetable(resultTable,filename,'Sheet',1,'Range','A1')
end